package fr.ensisa.hassenforder.game.server;

import java.io.IOException;
import java.net.Socket;
import java.util.Collection;

import fr.ensisa.hassenforder.game.model.Account;
import fr.ensisa.hassenforder.game.model.Product;
import fr.ensisa.hassenforder.game.model.User;
import fr.ensisa.hassenforder.network.FileHelper;
import fr.ensisa.hassenforder.network.Protocol;

public class SessionServer {

	private Socket connection;
	private Document document;
	
	public SessionServer (Document document, Socket connection) {
		this.document = document;
		this.connection = connection;
	}
	
	public boolean operate() {
		try {
			Writer writer = new Writer (connection.getOutputStream());
			Reader reader = new Reader (connection.getInputStream());
			reader.receive ();
			switch (reader.getType ()) {
			case 0 : return false; // socket closed
			case Protocol.REQU_CONNECT : 
				writer.repConnect(document.connect((String)reader.getParams().get(0),(String)reader.getParams().get(1)));
			break;
			case Protocol.REQU_DISCONNECT: 
				writer.repDisconnect(document.disconnect(reader.name,reader.id));
				break;
			case Protocol.REQU_STRATISTIQUE:
				writer.repStasitic(document.getStatistics((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
			case Protocol.REQU_ADD_CASH:
				writer.repAddCash(document.addCash((String)reader.getParams().get(0), (long)reader.getParams().get(1),(int)reader.getParams().get(2) ));
				break;
			case Protocol.REQU_PICTURE:
				System.out.println("une demande d'image en cour " + (String)reader.getParams().get(0));
				writer.repPicture((String)reader.getParams().get(0));
				break;
			case Protocol.REQU_PRODUCTS:
				writer.repProduit(document.getProducts((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
			case Protocol.REQU_SHOP:
				writer.repShop(document.getShop((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
			case Protocol.REQU_REFRESH:
				writer.repRefresh(document.refreshShop((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
			case Protocol.REQU_BUY:
				writer.repBuy(document.buyProduct((String)reader.getParams().get(0),(long)reader.getParams().get(1),(String)reader.getParams().get(2)));
				break;
			case Protocol.REQU_SELL:
				writer.repSell(document.sellProduct((String)reader.getParams().get(0),(long)reader.getParams().get(1),(String)reader.getParams().get(2)));
				break;
			case Protocol.REQU_CONSUME:
				writer.repConsume(document.consumeProducts((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
			case Protocol.REQU_CLEAR:
				writer.repClear(document.clearProducts((String)reader.getParams().get(0),(long)reader.getParams().get(1)));
				break;
				
				
			default: return false; // connection jammed
			}
			writer.send ();
			return true;
		} catch (IOException e) {
			return false;
		}
	}

}
